package com.degon.hsyl.customchatapp.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.degon.hsyl.customchatapp.R;
import com.facebook.drawee.view.SimpleDraweeView;

import io.rong.imlib.RongIMClient;

public class UserHomePageActivity extends BaseActivity {

    private ImageView backBtn;
    private TextView title;
    private SimpleDraweeView headPic;
    private RelativeLayout linkmanBtn;
    private RelativeLayout conversationBtn;

    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_home_page);

        initViews();

        Intent intent = this.getIntent();
        String userName = intent.getStringExtra("name");
        userId = intent.getStringExtra("userId");
        String headPortrait = intent.getStringExtra("headPortrait");
        String token = intent.getStringExtra("token");
        title.setText(userName);
        headPic.setImageURI(Uri.parse(headPortrait));

        //连接IM
        RongIMClient.connect(token, new RongIMClient.ConnectCallback() {
            @Override
            public void onDatabaseOpened(RongIMClient.DatabaseOpenStatus code) {
                //消息数据库打开，可以进入到主页面
            }

            @Override
            public void onSuccess(String s) {
                //连接成功
            }

            @Override
            public void onError(RongIMClient.ConnectionErrorCode errorCode) {
                if(errorCode.equals(RongIMClient.ConnectionErrorCode.RC_CONN_TOKEN_INCORRECT)) {
                    //从 APP 服务获取新 token，并重连
                } else {
                    //无法连接 IM 服务器，请根据相应的错误码作出对应处理
                }
            }
        });

        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        linkmanBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UserHomePageActivity.this, LinkmanListActivity.class);
                intent.putExtra("userId", userId);
                startActivity(intent);
            }
        });

        conversationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UserHomePageActivity.this, ConversationListActivity.class);
                intent.putExtra("userId", userId);
                intent.putExtra("token", token);
                startActivity(intent);
            }
        });

    }

    private void initViews(){
        backBtn = findViewById(R.id.主页_返回按钮);
        title = findViewById(R.id.主页_用户名);
        headPic = findViewById(R.id.主页_用户头像);
        linkmanBtn = findViewById(R.id.打开联系人);
        conversationBtn = findViewById(R.id.打开消息页);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //退出登录
        RongIMClient.getInstance().logout();
    }
}